package engine.systems.ui.slider
{
	import engine.classes.enumerations.Axis;
	import engine.classes.enumerations.SystemPriority;
	import engine.systems.update.GameSystem;
	import engine.utils.MathUtils;
	
	public class SliderSystem extends GameSystem
	{
		public function SliderSystem()
		{
			super(SliderNode, updateNode);
			
			this.priority = SystemPriority.UPDATE;
		}
		
		private function updateNode(node:SliderNode, time:Number):void
		{
			var min:Number;
			var max:Number;
			
			switch(node.draggable.axis)
			{
				case Axis.X:
					min = node.bounds.rect.left;
					max = node.bounds.rect.right;
					
					if(node.edge)
					{
						min += node.edge.left;
						max -= node.edge.right;
					}
					
					if(node.draggable.dragging)
					{
						node.percent.percent = MathUtils.toPercent(node.spatial.x, min, max, true);
					}
					else
					{
						node.spatial.x = (node.percent.percent * (max - min)) + min;
					}
					break;
				
				case Axis.Y:
					min = node.bounds.rect.top;
					max = node.bounds.rect.bottom;
					
					if(node.edge)
					{
						min += node.edge.top;
						max -= node.edge.bottom;
					}
					
					if(node.draggable.dragging)
					{
						node.percent.percent = MathUtils.toPercent(node.spatial.y, min, max, true);
					}
					else
					{
						node.spatial.y = (node.percent.percent * (max - min)) + min;
					}
					break;
			}
		}
	}
}